package com.zhongwang.mms.module.allocation.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.allocation.model.AllocationOrderModel;
import com.zhongwang.mms.module.allocation.service.AllocationOrderService;
import com.zhongwang.mms.module.allocation.service.AllocationOutWarService;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:调拨出库
 * @author: th
 * @time: 2020/8/17 14:30
 */
@Controller
@RequestMapping("/allocationOutWar")
public class AllocationOutWarController extends BaseController {

    //页面文件路径
    private String prefix = "allocation/allocationOutWar";
    @Autowired
    private AllocationOutWarService allocationOutWarService;
    @Autowired
    private AllocationOrderService allocationOrderService;
    @Autowired
    private AllocationOrderDao allocationOrderDao;
    @Autowired
    private HBasicService HBasicService;


    //加载页面
    @GetMapping("/page")
    @RequiresPermissions({"allocation:out:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 调拨出库页面数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"allocation:out:view"})
    public JsonResult list(AllocationOrderModel query){

        startPage();
        query.setOdrStatus(3);
        query.setOdrOutStatus(1);
        List<AllocationOrderModel> list = allocationOrderService.selectAllByQuery(query);
        return jsonResult(list);
    }

    /**
     * 选择出库单
     * @param model
     * @return
     */
    @GetMapping("/allocationOutOrderDetail/{odrId}")
    @RequiresPermissions({"allocation:out:view"})
    public String allocationOutOrderDetail(Model model,@PathVariable("odrId") Integer odrId) {

        AllocationOrderModel allocationOrder = allocationOrderDao.selectAllByPrimaryKey(odrId);

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("odrMakePerson", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("odrId",odrId);
        model.addAttribute("dept", HBasicService.getDept());

        if(allocationOrder.getOdrType() == 1 || allocationOrder.getOdrType() == 3){
            return prefix + "/purOutPage";
        }else {

            return prefix + "/matOutPage";
        }
    }


    @ResponseBody
    @PostMapping("/detailList")
    //@RequiresPermissions({"halwar:halallinwar:view"})
    public JsonResult detailList(Integer odrId){

        startPage();
        List<AllocationOrderModel> list = allocationOutWarService.selectOrderDetailByOdrId(odrId);
        return jsonResult(list);
    }

    /**
     * 出库操作
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/saveAllocationOutWar")
    @RequiresPermissions({"allocation:out:edit"})
    public JsonResult saveAllocationOutWar(AllocationOrderModel param){

        return allocationOutWarService.saveAllocationOutWar(param);
    }
}
